package com.aiyige.page.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.page.login.presenter.ResetPasswordPresenter;
import com.aiyige.page.login.view.IResetPasswordView;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置新密码
 */
@Route(path = ARouterConfig.ResetPasswordPage)
public class ResetPasswordPage extends BaseActivity implements IResetPasswordView {
    public static final String TAG = ResetPasswordPage.class.getSimpleName();
    @BindView(R.id.iv_password_visible)
    ImageView ivPasswordVisible;

    @BindView(R.id.tv_username)
    TextView tvUserName;
    @BindView(R.id.et_login_password)
    EditText etLoginPassword;

    @BindView(R.id.btn_finish)
    Button btnFinish;

    boolean isPasswordVisible = false;

    private ResetPasswordPresenter presenter = new ResetPasswordPresenter(this);

    @Autowired
    public String mobile;

    @Autowired
    public String code;

    public static void start(String mobile,String code) {
        ARouter.getInstance().build(ARouterConfig.ResetPasswordPage).withString("mobile",mobile)
                .withString("code",code).navigation();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_resetpassword);
        ARouter.getInstance().inject(this);
        ButterKnife.bind(this);
        etLoginPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkPassword(s.toString());
            }
        });
        if (isEmpty(mobile)) {
            showToast("mobile is empty");
            finish();
            return;
        }
        etLoginPassword.setImeOptions(EditorInfo.IME_ACTION_DONE);
        etLoginPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event)  {
                if (actionId==EditorInfo.IME_ACTION_DONE
                        ||(event!=null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {
                    presenter.netResetPassword();
                    return true;
                }
                return false;
            }
        });
        tvUserName.setText(mobile);
        checkPassword(null);
    }

    private void checkPassword(String password) {
        btnFinish.setEnabled(!isEmpty(password));
    }

    @Override
    public String getMobile() {
        return tvUserName.getText().toString();
    }

    @Override
    public String getPassword() {
        return etLoginPassword.getText().toString();
    }

    @Override
    public void showLoading() {
        super.showLoading();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
    }

    @Override
    public String getCode() {
        return code;
    }

    @OnClick({R.id.rl_back, R.id.iv_password_visible, R.id.rl_password_visible, R.id.btn_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.iv_password_visible:
            case R.id.rl_password_visible:
                isPasswordVisible = !isPasswordVisible;
                ivPasswordVisible.setImageResource(isPasswordVisible?R.drawable.login_eye_y:R.drawable.login_eye_n);
                etLoginPassword.setTransformationMethod(isPasswordVisible? HideReturnsTransformationMethod.getInstance(): PasswordTransformationMethod.getInstance());
                etLoginPassword.setSelection(isEmpty(getPassword())?0:getPassword().length());
                break;
            case R.id.btn_finish:
                if (!TextUtils.isEmpty(getPassword()) && getPassword().length() < 6) {
                    ToastX.show(getString(R.string.password_length_is_not_correct));
                    return;
                }
                presenter.netResetPassword();
                break;
        }
    }
}
